# Definition for singly-linked list.
class ListNode:
  def __init__(self, x):
    self.val = x
    self.next = None


class Solution:
  def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
    vals = []
    head_sav = head
    while head is not None:
      vals.append(head.val)
      head = head.next
    for i in range(len(vals) - k):
      head_sav = head_sav.next
    return head_sav


'''
STATUS: SOLVED
TRIES:  1
TIME:   44 MS
MEMORY: 13.8 MB
'''
if __name__ == '__main__':
  solution = Solution()
  nodes = [1, 2, 3, 4, 5]
  head = ListNode(1)
  head_sav = head
  for i in range(1, len(nodes)):
    head.next = ListNode(nodes[i])
    head = head.next
  print(solution.getKthFromEnd(head=head_sav, k=2).val)
